package com.touch.jgloo;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
/**
 * An Object that counts the FPS and draws them to the screen 
 * @author Homicidal Monkey
 * @version 10.1.9
 * @since r461
 * @see com/touch/Licence
 */
public class JGLFPSCounter implements GLEventListener 
{
	public static final byte ENABLE_FPS_COUNT = 1;	
	public static final byte PRINT_FPS_COUNT = 2;
	public static final byte ENABLE_FPS_LOCK = 4;
	private int count = 0;
	private long time = System.currentTimeMillis();	
	private float interval = 5f;
	protected byte status = ENABLE_FPS_COUNT | ENABLE_FPS_LOCK;
	private float fps;
	public JGLFPSCounter(){}
	public JGLFPSCounter(byte status)
	{
		this.status = status;
	}
	public void display(final GLAutoDrawable drawable) 
	{	
		if((status & ENABLE_FPS_COUNT) == 0)
			return;
		//increase the count value
		this.count ++;
		//calculate the current fps
		if(System.currentTimeMillis() - this.time > this.interval * 1000)
		{
			this.time = System.currentTimeMillis();
			this.fps = count / interval;
			if((status & PRINT_FPS_COUNT) == PRINT_FPS_COUNT)
				System.out.println("FPS: " + count / interval);
			this.count = 0;
		}	
	}	
	public void setStatus(byte status)
	{
		this.status = status;
	}
	public float getFPS()
	{
		return this.fps;
	}
	public void setInterval(float interval)
	{
		this.interval = interval;
	}
	public void displayChanged(GLAutoDrawable arg0, boolean arg1, boolean arg2) {}

	public void init(GLAutoDrawable arg0) 
	{
		if((status & ENABLE_FPS_COUNT) == ENABLE_FPS_COUNT)
			System.out.println("calculating fps...");
	}

	public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
			int arg4) {
		// TODO Auto-generated method stub
		
	}

}
